﻿
    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public partial class WebPage_upload_pic : Page
    {
   

        protected void AddWater(string Path, string Path_sy)
        {
            string s = "usfang.com";
            System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
            Graphics graphics = Graphics.FromImage(image);
            graphics.DrawImage(image, 0, 0, image.Width, image.Height);
            Font font = new Font("Verdana", 60f);
            Brush brush = new SolidBrush(Color.Green);
            graphics.DrawString(s, font, brush, (float)35f, (float)35f);
            graphics.Dispose();
            image.Save(Path_sy);
            image.Dispose();
        }

        public void AddWaterPic(string Path, string Path_syp, string Path_sypf)
        {
            System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
            System.Drawing.Image image2 = System.Drawing.Image.FromFile(Path_sypf);
            Graphics graphics = Graphics.FromImage(image);
            graphics.DrawImage(image2, new Rectangle(image.Width - image2.Width, image.Height - image2.Height, image2.Width, image2.Height), 0, 0, image2.Width, image2.Height, GraphicsUnit.Pixel);
            graphics.Dispose();
            image.Save(Path_syp);
            image.Dispose();
            File.Delete(Path);
        }

        protected void btnupload_Click(object sender, EventArgs e)
        {
            bool flag = false;
            int year = DateTime.Now.Year;
            int month = DateTime.Now.Month;
            int day = DateTime.Now.Day;
            string path = string.Concat(new object[] { "/upfile/logo/", base.Request["type"], "/", year, "/", month, "/", day, "/" });
            string str2 = base.Server.MapPath(path);
            if (!Directory.Exists(str2))
            {
                Directory.CreateDirectory(str2);
            }
            if (this.cstp.HasFile)
            {
                HttpPostedFile file = base.Request.Files[0];
                if (file.ContentLength > 0x1e8480)
                {
                    base.Response.Write("<script>alert('您上传的图片太大,请修改为小于2000k后重新上传');window.location.href='upload_pic.aspx';</script>");
                    base.Response.End();
                }
                else
                {
                    string str3 = Path.GetExtension(this.cstp.FileName).ToLower();
                    string[] strArray = new string[] { ".gif", ".png", ".bmp", ".jpg", ".jpeg" };
                    for (int i = 0; i < strArray.Length; i++)
                    {
                        if (str3 == strArray[i])
                        {
                            flag = true;
                        }
                    }
                }
            }
            if (flag)
            {
                try
                {
                    Random random = new Random();
                    string str4 = random.Next(0x3e8, 0x270f).ToString();
                    string str5 = DateTime.Now.ToString("yyyyMMddhhmmss");
                    string[] strArray2 = this.cstp.FileName.Split(new char[] { '.' });
                    int length = strArray2.Length;
                    string str7 = strArray2[length - 1];
                    Bitmap bitmap = new Bitmap(System.Drawing.Image.FromStream(this.cstp.PostedFile.InputStream));
                    this.cstp.SaveAs(str2 + str5 + str4 + "." + str7);
                    this.lb2.Text = "上传图片成功!";
                    string str8 = path + str5 + str4 + "." + str7;
                    string str9 = path + str5 + str4 + "_s." + str7;
                    string str10 = path + str5 + str4 + "_b." + str7;
                    try
                    {
                        this.MakeThumbnail(base.Server.MapPath(str8), base.Server.MapPath(str9), 140, 0, "W");
                        this.MakeThumbnail(base.Server.MapPath(str8), base.Server.MapPath(str10), 400, 0, "W");
                    }
                    catch (Exception exception)
                    {
                        base.Response.Write(exception);
                    }
                    base.Response.Write("<script language='javascript'>window.parent.document.getElementById('hidImgurl').value='" + str8 + "';</script>");
                    base.Response.Write("<script language='javascript'>window.parent.document.getElementById('Image2').src='" + str8 + "';</script>");
                    this.hidImgurl.Value = str8;
                }
                catch
                {
                    this.lb2.Text = "上传图片失败！";
                }
            }
            else
            {
                this.lb2.Text = "上传图片类型不合法";
            }
        }

        public void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
        {
            System.Drawing.Image image = System.Drawing.Image.FromFile(originalImagePath);
            int num = width;
            int num2 = height;
            int x = 0;
            int y = 0;
            int num5 = image.Width;
            int num6 = image.Height;
            string str = mode;
            if ((str != null) && (str != "HW"))
            {
                if (!(str == "W"))
                {
                    if (str == "H")
                    {
                        num = (image.Width * height) / image.Height;
                    }
                    else if (str == "Cut")
                    {
                        if ((((double)image.Width) / ((double)image.Height)) > (((double)num) / ((double)num2)))
                        {
                            num6 = image.Height;
                            num5 = (image.Height * num) / num2;
                            y = 0;
                            x = (image.Width - num5) / 2;
                        }
                        else
                        {
                            num5 = image.Width;
                            num6 = (image.Width * height) / num;
                            x = 0;
                            y = (image.Height - num6) / 2;
                        }
                    }
                }
                else
                {
                    num2 = (image.Height * width) / image.Width;
                }
            }
            System.Drawing.Image image2 = new Bitmap(num, num2);
            Graphics graphics = Graphics.FromImage(image2);
            graphics.InterpolationMode = InterpolationMode.High;
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.Clear(Color.Transparent);
            graphics.DrawImage(image, new Rectangle(0, 0, num, num2), new Rectangle(x, y, num5, num6), GraphicsUnit.Pixel);
            try
            {
                image2.Save(thumbnailPath, ImageFormat.Jpeg);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            finally
            {
                image.Dispose();
                image2.Dispose();
                graphics.Dispose();
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }
    }


